***************
* Loading data*
***************

**** Load the data

*********************
* Preparing the data*
*********************

* Variables: adopt_dm, adopt_legal
* Description: Whether the parliament adopted (ie. use it at t0, while didn't use it at t-1) low district magnitudes and high electoral thresholds by year

by country (year), sort: gen byte adopt_dm = sum(dv_dm == 1) == 1  & sum(dv_dm[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_dm = dv_dm[_n-1]
replace adopt_dm=1 if lag_dv_dm==0 & dv_dm==1

by country (year), sort: gen byte adopt_legal = sum(dv_legal == 1) == 1  & sum(dv_legal[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_legal = dv_legal[_n-1]
replace adopt_legal=1 if lag_dv_legal==0 & dv_legal==1

replace adopt_dm=0 if country=="Belgium" & year==1946
replace adopt_legal=0 if country=="Belgium" & year==1946
replace adopt_dm=0 if country=="Denmark" & year==1947
replace adopt_legal=0 if country=="Denmark" & year==1947
replace adopt_dm=0 if country=="Finland" & year==1948
replace adopt_legal=0 if country=="Finland" & year==1948
replace adopt_dm=0 if country=="Luxembourg" & year==1945
replace adopt_legal=0 if country=="Luxembourg" & year==1945
replace adopt_dm=0 if country=="Netherlands" & year==1946
replace adopt_legal=0 if country=="Netherlands" & year==1946
replace adopt_dm=0 if country=="Sweden" & year==1948
replace adopt_legal=0 if country=="Sweden" & year==1948
replace adopt_dm=0 if country=="Switzerland" & year==1948
replace adopt_legal=0 if country=="Switerland" & year==1948

* Variables: number_dm, number_legal
* Descritpion: Number of times low district magnitudes and high electoral thresholds have been used previously since 1945 (missing if low district magnitudes and high electoral threshold currently in use)
 
gen number_dm=.
replace number_dm=1 if year>=1970 & year<=1994 & country=="Austria"
replace number_dm=1 if year>=1985 & country=="Belgium"
replace number_dm=1 if year>=1991 & country=="Bulgaria"
replace number_dm=0 if country=="Croatia"
replace number_dm=1 if year>=1985 & country=="Cyrpus"
replace number_dm=0 if country=="Czech Republic"
replace number_dm=1 if year>=1971 & year<=1997 & country=="Denmark"
replace number_dm=0 if country=="Estonia"
replace number_dm=0 if country=="Finland"
replace number_dm=0 if country=="Germany"
replace number_dm=0 if country=="Italy" & year<=1994
replace number_dm=1 if country=="Italy" & year>=2006
replace number_dm=0 if country=="Latvia"
replace number_dm=0 if country=="Luxembourg"
replace number_dm=0 if country=="Netherlands"
replace number_dm=1 if year>=1973 & country=="Norway"
replace number_dm=0 if year<=1993 & country=="Poland"
replace number_dm=1 if year>=2001 & country=="Poland"
replace number_dm=0 if country=="Portugal"
replace number_dm=0 if country=="Romania"
replace number_dm=0 if country=="Slovakia"
replace number_dm=0 if country=="Slovenia"
replace number_dm=0 if country=="Sweden"
replace number_dm=0 if country=="Switzerland"

gen number_legal=.
replace number_legal=0 if country=="Austria"
replace number_legal=0 if year<=2003 & country=="Belgium"
replace number_legal=0 if country=="Bulgaria"
replace number_legal=0 if year<=1995 & country=="Croatia"
replace number_legal=1 if year>=1996 & country=="Cyprus"
replace number_legal=0 if country=="Denmark"
replace number_legal=0 if country=="Finland"
replace number_legal=1 if year>=1986 & country=="Greece"
replace number_legal=0 if year<=1994 & country=="Hungary"
replace number_legal=0 if year<=2003 & country=="Iceland"
replace number_legal=0 if country=="Italy"
replace number_legal=0 if year<=1995 & country=="Latvia"
replace number_legal=0 if year<=1996 & country=="Lithuania"
replace number_legal=0 if country=="Luxembourg"
replace number_legal=0 if country=="Netherlands"
replace number_legal=0 if country=="Norway"
replace number_legal=0 if year<=1993 & country=="Poland"
replace number_legal=0 if country=="Portugal"
replace number_legal=0 if year<=2000 & country=="Romania"
replace number_legal=0 if country=="Slovenia"
replace number_legal=0 if country=="Spain"
replace number_legal=0 if country=="Sweden"
replace number_legal=0 if country=="Switzerland"

* Variables: group_ling, group_demo, group_bound_[country]
* Description: Groups of linguistic, historical and geographical peers to which the democracy belongs

gen group_ling=.
replace group_ling=1 if country=="Italy"
replace group_ling=1 if country=="Portugal"
replace group_ling=1 if country=="Romania"
replace group_ling=1 if country=="Spain"
replace group_ling=2 if country=="Germany" 
replace group_ling=2 if country=="Austria" 
replace group_ling=2 if country=="Switzerland"
replace group_ling=2 if country=="Norway"
replace group_ling=2 if country=="Sweden"
replace group_ling=2 if country=="Iceland"
replace group_ling=2 if country=="Netherlands"
replace group_ling=2 if country=="Belgium"
replace group_ling=2 if country=="Luxembourg"
replace group_ling=2 if country=="Denmark"
replace group_ling=3 if country=="Czech Republic"
replace group_ling=3 if country=="Slovakia"
replace group_ling=3 if country=="Poland"
replace group_ling=3 if country=="Slovenia"
replace group_ling=3 if country=="Croatia"
replace group_ling=3 if country=="Bulgaria"
replace group_ling=3 if country=="Latvia"
replace group_ling=3 if country=="Lithuania" 
replace group_ling=4 if country=="Greece"
replace group_ling=4 if country=="Cyprus"
replace group_ling=5 if country=="Hungary"
replace group_ling=5 if country=="Finland"
replace group_ling=5 if country=="Estonia"

gen group_demo=.
replace group_demo=1 if country=="Czech Republic"
replace group_demo=1 if country=="Slovakia"
replace group_demo=1 if country=="Poland"
replace group_demo=1 if country=="Slovenia"
replace group_demo=1 if country=="Hungary"
replace group_demo=1 if country=="Croatia"
replace group_demo=1 if country=="Bulgaria"
replace group_demo=1 if country=="Latvia"
replace group_demo=1 if country=="Lithuania"
replace group_demo=1 if country=="Estonia"
replace group_demo=1 if country=="Romania"
replace group_demo=2 if country=="Cyprus"
replace group_demo=2 if country=="Greece"
replace group_demo=2 if country=="Portugal"
replace group_demo=2 if country=="Spain"
replace group_demo=3 if country=="Austria"
replace group_demo=3 if country=="Switzerland"
replace group_demo=3 if country=="Norway"
replace group_demo=3 if country=="Sweden"
replace group_demo=3 if country=="Iceland"
replace group_demo=3 if country=="Netherlands"
replace group_demo=3 if country=="Belgium"
replace group_demo=3 if country=="Luxembourg"
replace group_demo=3 if country=="Denmark"
replace group_demo=3 if country=="Finland"
replace group_demo=3 if country=="Germany"
replace group_demo=3 if country=="Italy"

gen group_bound_aus=.
replace group_bound_aus=1 if country=="Austria"
replace group_bound_aus=1 if country=="Czech Republic"
replace group_bound_aus=1 if country=="Germany"
replace group_bound_aus=1 if country=="Hungary"
replace group_bound_aus=1 if country=="Italy"
replace group_bound_aus=1 if country=="Slovakia"
replace group_bound_aus=1 if country=="Slovenia"
replace group_bound_aus=1 if country=="Switzerland"

gen group_bound_bel=.
replace group_bound_bel=1 if country=="Belgium"
replace group_bound_bel=1 if country=="Germany"
replace group_bound_bel=1 if country=="Netherlands"
replace group_bound_bel=1 if country=="Luxembourg"

gen group_bound_bul=.
replace group_bound_bul=1 if country=="Bulgaria"
replace group_bound_bul=1 if country=="Greece"
replace group_bound_bul=1 if country=="Romania"

gen group_bound_cro=.
replace group_bound_cro=1 if country=="Croatia"
replace group_bound_cro=1 if country=="Hungary"
replace group_bound_cro=1 if country=="Slovenia"
replace group_bound_cro=1 if country=="Italy"

gen group_bound_cyp=.
replace group_bound_cyp=1 if country=="Cyprus"
replace group_bound_cyp=1 if country=="Greece"

gen group_bound_cze=.
replace group_bound_cze=1 if country=="Czech Republic"
replace group_bound_cze=1 if country=="Slovakia"
replace group_bound_cze=1 if country=="Poland"
replace group_bound_cze=1 if country=="Hungary"
replace group_bound_cze=1 if country=="Germany"

gen group_bound_den=.
replace group_bound_den=1 if country=="Denmark"
replace group_bound_den=1 if country=="Germany"
replace group_bound_den=1 if country=="Norway"
replace group_bound_den=1 if country=="Sweden"
replace group_bound_den=1 if country=="Poland"

gen group_bound_est=.
replace group_bound_est=1 if country=="Estonia"
replace group_bound_est=1 if country=="Finland"
replace group_bound_est=1 if country=="Latvia"
replace group_bound_est=1 if country=="Sweden"

gen group_bound_fin=.
replace group_bound_fin=1 if country=="Finland"
replace group_bound_fin=1 if country=="Sweden"
replace group_bound_fin=1 if country=="Norway"
replace group_bound_fin=1 if country=="Estonia"

gen group_bound_ger=.
replace group_bound_ger=1 if country=="Germany"
replace group_bound_ger=1 if country=="Netherlands"
replace group_bound_ger=1 if country=="Belgium"
replace group_bound_ger=1 if country=="Luxembourg"
replace group_bound_ger=1 if country=="Czech Republic"
replace group_bound_ger=1 if country=="Austria"
replace group_bound_ger=1 if country=="Denmark"
replace group_bound_ger=1 if country=="Poland"
replace group_bound_ger=1 if country=="Switzerland"
replace group_bound_ger=1 if country=="Sweden"

gen group_bound_gre=.
replace group_bound_gre=1 if country=="Greece"
replace group_bound_gre=1 if country=="Bulgaria"
replace group_bound_gre=1 if country=="Cyprus"
replace group_bound_gre=1 if country=="Italy"

gen group_bound_hun=.
replace group_bound_hun=1 if country=="Hungary"
replace group_bound_hun=1 if country=="Romania"
replace group_bound_hun=1 if country=="Slovakia"
replace group_bound_hun=1 if country=="Austria"
replace group_bound_hun=1 if country=="Croatia"
replace group_bound_hun=1 if country=="Slovenia"

gen group_bound_ice=.

gen group_bound_ita=.
replace group_bound_ita=1 if country=="Italy"
replace group_bound_ita=1 if country=="Switzerland"
replace group_bound_ita=1 if country=="Slovenia"
replace group_bound_ita=1 if country=="Austria"
replace group_bound_ita=1 if country=="Greece"
replace group_bound_ita=1 if country=="Spain"
replace group_bound_ita=1 if country=="Croatia"

gen group_bound_lat=.
replace group_bound_lat=1 if country=="Latvia"
replace group_bound_lat=1 if country=="Estonia"
replace group_bound_lat=1 if country=="Lithuania"
replace group_bound_lat=1 if country=="Sweden"

gen group_bound_lit=.
replace group_bound_lit=1 if country=="Lithuania"
replace group_bound_lit=1 if country=="Latvia"
replace group_bound_lit=1 if country=="Poland"
replace group_bound_lit=1 if country=="Sweden"

gen group_bound_lux=.
replace group_bound_lux=1 if country=="Luxembourg"
replace group_bound_lux=1 if country=="Belgium"
replace group_bound_lux=1 if country=="Germany"

gen group_bound_net=.
replace group_bound_net=1 if country=="Netherlands"
replace group_bound_net=1 if country=="Germany"
replace group_bound_net=1 if country=="Belgium"

gen group_bound_nor=.
replace group_bound_nor=1 if country=="Norway"
replace group_bound_nor=1 if country=="Sweden"
replace group_bound_nor=1 if country=="Iceland"
replace group_bound_nor=1 if country=="Finland"

gen group_bound_pol=.
replace group_bound_pol=1 if country=="Poland"
replace group_bound_pol=1 if country=="Germany"
replace group_bound_pol=1 if country=="Czech Republic"
replace group_bound_pol=1 if country=="Slovakia"
replace group_bound_pol=1 if country=="Lithuania"
replace group_bound_pol=1 if country=="Sweden"
replace group_bound_pol=1 if country=="Denmark"

gen group_bound_por=.
replace group_bound_por=1 if country=="Portugal"
replace group_bound_por=1 if country=="Spain"

gen group_bound_rom=.
replace group_bound_rom=1 if country=="Romania"
replace group_bound_rom=1 if country=="Bulgaria"
replace group_bound_rom=1 if country=="Hungary"

gen group_bound_slo=.
replace group_bound_slo=1 if country=="Slovakia"
replace group_bound_slo=1 if country=="Czech Republic"
replace group_bound_slo=1 if country=="Austria"
replace group_bound_slo=1 if country=="Hungary"
replace group_bound_slo=1 if country=="Poland"

gen group_bound_slove=.
replace group_bound_slove=1 if country=="Slovenia"
replace group_bound_slove=1 if country=="Croatia"
replace group_bound_slove=1 if country=="Italy"
replace group_bound_slove=1 if country=="Austria"
replace group_bound_slove=1 if country=="Hungary"

gen group_bound_spa=.
replace group_bound_spa=1 if country=="Spain"
replace group_bound_spa=1 if country=="Portugal"
replace group_bound_spa=1 if country=="Italy"

gen group_bound_swe=.
replace group_bound_swe=1 if country=="Sweden"
replace group_bound_swe=1 if country=="Norway"
replace group_bound_swe=1 if country=="Denmark"
replace group_bound_swe=1 if country=="Finland"
replace group_bound_swe=1 if country=="Germany"
replace group_bound_swe=1 if country=="Latvia"
replace group_bound_swe=1 if country=="Lithuania"
replace group_bound_swe=1 if country=="Poland"
replace group_bound_swe=1 if country=="Estonia"

gen group_bound_swi=.
replace group_bound_swi=1 if country=="Switzerland"
replace group_bound_swi=1 if country=="Germany"
replace group_bound_swi=1 if country=="Austria"
replace group_bound_swi=1 if country=="Italy"

* Variables: n_peers_ling, n_peers_demo, n_peers_bound
* Description: Number of democracies in each of the linguistic, historical and geographical group of peers by year

gen n=1

bysort year group_ling: egen n_peers_ling = total(n)
replace n_peers_ling=n_peers_ling-1

bysort year group_demo: egen n_peers_demo = total(n)
replace n_peers_demo=n_peers_demo-1

bysort year group_bound_aus: egen n_peers_bound_aus = total(n) if group_bound_aus==1
replace n_peers_bound_aus = n_peers_bound_aus-1
bysort year group_bound_bel: egen n_peers_bound_bel = total(n) if group_bound_bel==1
replace n_peers_bound_bel = n_peers_bound_bel-1
bysort year group_bound_bul: egen n_peers_bound_bul = total(n) if group_bound_bul==1
replace n_peers_bound_bul = n_peers_bound_bul-1
bysort year group_bound_cro: egen n_peers_bound_cro = total(n) if group_bound_cro==1
replace n_peers_bound_cro = n_peers_bound_cro-1
bysort year group_bound_cyp: egen n_peers_bound_cyp = total(n) if group_bound_cyp==1
replace n_peers_bound_cyp = n_peers_bound_cyp-1
bysort year group_bound_cze: egen n_peers_bound_cze = total(n) if group_bound_cze==1
replace n_peers_bound_cze = n_peers_bound_cze-1
bysort year group_bound_den: egen n_peers_bound_den = total(n) if group_bound_den==1
replace n_peers_bound_den = n_peers_bound_den-1
bysort year group_bound_est: egen n_peers_bound_est = total(n) if group_bound_est==1
replace n_peers_bound_est = n_peers_bound_est-1
bysort year group_bound_fin: egen n_peers_bound_fin = total(n) if group_bound_fin==1
replace n_peers_bound_fin = n_peers_bound_fin-1
bysort year group_bound_ger: egen n_peers_bound_ger = total(n) if group_bound_ger==1
replace n_peers_bound_ger = n_peers_bound_ger-1
bysort year group_bound_gre: egen n_peers_bound_gre = total(n) if group_bound_gre==1
replace n_peers_bound_gre = n_peers_bound_gre-1
bysort year group_bound_hun: egen n_peers_bound_hun = total(n) if group_bound_hun==1
replace n_peers_bound_hun = n_peers_bound_hun-1
bysort year group_bound_ice: egen n_peers_bound_ice = total(n) if group_bound_ice==1
replace n_peers_bound_ice = n_peers_bound_ice-1
bysort year group_bound_ita: egen n_peers_bound_ita = total(n) if group_bound_ita==1
replace n_peers_bound_ita = n_peers_bound_ita-1
bysort year group_bound_lat: egen n_peers_bound_lat = total(n) if group_bound_lat==1
replace n_peers_bound_lat = n_peers_bound_lat-1
bysort year group_bound_lit: egen n_peers_bound_lit = total(n) if group_bound_lit==1
replace n_peers_bound_lit = n_peers_bound_lit-1
bysort year group_bound_lux: egen n_peers_bound_lux = total(n) if group_bound_lux==1
replace n_peers_bound_lux = n_peers_bound_lux-1
bysort year group_bound_net: egen n_peers_bound_net = total(n) if group_bound_net==1
replace n_peers_bound_net = n_peers_bound_net-1
bysort year group_bound_nor: egen n_peers_bound_nor = total(n) if group_bound_nor==1
replace n_peers_bound_nor = n_peers_bound_nor-1
bysort year group_bound_pol: egen n_peers_bound_pol = total(n) if group_bound_pol==1
replace n_peers_bound_pol = n_peers_bound_pol-1
bysort year group_bound_por: egen n_peers_bound_por = total(n) if group_bound_por==1
replace n_peers_bound_por = n_peers_bound_por-1
bysort year group_bound_rom: egen n_peers_bound_rom = total(n) if group_bound_rom==1
replace n_peers_bound_rom = n_peers_bound_rom-1
bysort year group_bound_slo: egen n_peers_bound_slo = total(n) if group_bound_slo==1
replace n_peers_bound_slo = n_peers_bound_slo-1
bysort year group_bound_slove: egen n_peers_bound_slove = total(n) if group_bound_slove==1
replace n_peers_bound_slove = n_peers_bound_slove-1
bysort year group_bound_spa: egen n_peers_bound_spa = total(n) if group_bound_spa==1
replace n_peers_bound_spa = n_peers_bound_spa-1
bysort year group_bound_swe: egen n_peers_bound_swe = total(n) if group_bound_swe==1
replace n_peers_bound_swe = n_peers_bound_swe-1
bysort year group_bound_swi: egen n_peers_bound_swi = total(n) if group_bound_swi==1
replace n_peers_bound_swi = n_peers_bound_swi

gen n_peers_bound=.
replace n_peers_bound=n_peers_bound_aus if country=="Austria"
replace n_peers_bound=n_peers_bound_bel if country=="Belgium"
replace n_peers_bound=n_peers_bound_bul if country=="Bulgaria"
replace n_peers_bound=n_peers_bound_cro if country=="Croatia"
replace n_peers_bound=n_peers_bound_cyp if country=="Cyprus"
replace n_peers_bound=n_peers_bound_cze if country=="Czech Republic"
replace n_peers_bound=n_peers_bound_den if country=="Denmark"
replace n_peers_bound=n_peers_bound_est if country=="Estonia"
replace n_peers_bound=n_peers_bound_fin if country=="Finland"
replace n_peers_bound=n_peers_bound_ger if country=="Germany"
replace n_peers_bound=n_peers_bound_gre if country=="Greece"
replace n_peers_bound=n_peers_bound_hun if country=="Hungary"
replace n_peers_bound=n_peers_bound_ice if country=="Iceland"
replace n_peers_bound=n_peers_bound_ita if country=="Italy"
replace n_peers_bound=n_peers_bound_lat if country=="Latvia"
replace n_peers_bound=n_peers_bound_lit if country=="Lithuania"
replace n_peers_bound=n_peers_bound_lux if country=="Luxembourg"
replace n_peers_bound=n_peers_bound_net if country=="Netherlands"
replace n_peers_bound=n_peers_bound_nor if country=="Norway"
replace n_peers_bound=n_peers_bound_pol if country=="Poland"
replace n_peers_bound=n_peers_bound_por if country=="Portugal"
replace n_peers_bound=n_peers_bound_rom if country=="Romania"
replace n_peers_bound=n_peers_bound_slo if country=="Slovakia"
replace n_peers_bound=n_peers_bound_slove if country=="Slovenia"
replace n_peers_bound=n_peers_bound_spa if country=="Spain"
replace n_peers_bound=n_peers_bound_swe if country=="Sweden"
replace n_peers_bound=n_peers_bound_swi if country=="Switzerland"

* Variables: dm_ling, dm_demo, dm_bound, legal_ling, legal_demo, legal_bound
* Description: Proportion of linguistic, historical and geographical peers that have adopted low district magnitudes and high electoral thresholds by year

bysort year group_ling: egen dm_ling=total(adopt_dm)
bysort year group_demo: egen dm_demo=total(adopt_dm)
bysort year group_bound_aus: egen dm_bound_aus= total(adopt_dm)
bysort year group_bound_bel: egen dm_bound_bel= total(adopt_dm)
bysort year group_bound_bul: egen dm_bound_bul= total(adopt_dm)
bysort year group_bound_cro: egen dm_bound_cro= total(adopt_dm)
bysort year group_bound_cyp: egen dm_bound_cyp= total(adopt_dm)
bysort year group_bound_cze: egen dm_bound_cze= total(adopt_dm)
bysort year group_bound_den: egen dm_bound_den= total(adopt_dm)
bysort year group_bound_est: egen dm_bound_est= total(adopt_dm)
bysort year group_bound_fin: egen dm_bound_fin= total(adopt_dm)
bysort year group_bound_ger: egen dm_bound_ger= total(adopt_dm)
bysort year group_bound_gre: egen dm_bound_gre= total(adopt_dm)
bysort year group_bound_hun: egen dm_bound_hun= total(adopt_dm)
bysort year group_bound_ice: egen dm_bound_ice= total(adopt_dm)
bysort year group_bound_ita: egen dm_bound_ita= total(adopt_dm)
bysort year group_bound_lat: egen dm_bound_lat= total(adopt_dm)
bysort year group_bound_lit: egen dm_bound_lit= total(adopt_dm)
bysort year group_bound_lux: egen dm_bound_lux= total(adopt_dm)
bysort year group_bound_net: egen dm_bound_net= total(adopt_dm)
bysort year group_bound_nor: egen dm_bound_nor= total(adopt_dm)
bysort year group_bound_pol: egen dm_bound_pol= total(adopt_dm)
bysort year group_bound_por: egen dm_bound_por= total(adopt_dm)
bysort year group_bound_rom: egen dm_bound_rom= total(adopt_dm)
bysort year group_bound_slo: egen dm_bound_slo= total(adopt_dm)
bysort year group_bound_slove: egen dm_bound_slove= total(adopt_dm)
bysort year group_bound_spa: egen dm_bound_spa= total(adopt_dm)
bysort year group_bound_swe: egen dm_bound_swe= total(adopt_dm)
bysort year group_bound_swi: egen dm_bound_swi= total(adopt_dm)

gen dm_bound=.
replace dm_bound=dm_bound_aus if country=="Austria"
replace dm_bound=dm_bound_bel if country=="Belgium"
replace dm_bound=dm_bound_bul if country=="Bulgaria"
replace dm_bound=dm_bound_cro if country=="Croatia"
replace dm_bound=dm_bound_cyp if country=="Cyprus"
replace dm_bound=dm_bound_cze if country=="Czech Republic"
replace dm_bound=dm_bound_den if country=="Denmark"
replace dm_bound=dm_bound_est if country=="Estonia"
replace dm_bound=dm_bound_fin if country=="Finland"
replace dm_bound=dm_bound_ger if country=="Germany"
replace dm_bound=dm_bound_gre if country=="Greece"
replace dm_bound=dm_bound_hun if country=="Hungary"
replace dm_bound=dm_bound_ice if country=="Iceland"
replace dm_bound=dm_bound_ita if country=="Italy"
replace dm_bound=dm_bound_lat if country=="Latvia"
replace dm_bound=dm_bound_lit if country=="Lithuania"
replace dm_bound=dm_bound_lux if country=="Luxembourg"
replace dm_bound=dm_bound_net if country=="Netherlands"
replace dm_bound=dm_bound_nor if country=="Norway"
replace dm_bound=dm_bound_pol if country=="Poland"
replace dm_bound=dm_bound_por if country=="Portugal"
replace dm_bound=dm_bound_rom if country=="Romania"
replace dm_bound=dm_bound_slo if country=="Slovakia"
replace dm_bound=dm_bound_slove if country=="Slovenia"
replace dm_bound=dm_bound_spa if country=="Spain"
replace dm_bound=dm_bound_swe if country=="Sweden"
replace dm_bound=dm_bound_swi if country=="Switzerland"

replace dm_ling=dm_ling-1 if adopt_dm==1
replace dm_demo=dm_demo-1 if adopt_dm==1
replace dm_bound=dm_bound-1 if adopt_dm==1

replace dm_ling=dm_ling/n_peers_ling
replace dm_demo=dm_demo/n_peers_demo
replace dm_bound=dm_bound/n_peers_bound

bysort year group_ling: egen legal_ling=total(adopt_legal)
bysort year group_demo: egen legal_demo=total(adopt_legal)
bysort year group_bound_aus: egen legal_bound_aus= total(adopt_legal)
bysort year group_bound_bel: egen legal_bound_bel= total(adopt_legal)
bysort year group_bound_bul: egen legal_bound_bul= total(adopt_legal)
bysort year group_bound_cro: egen legal_bound_cro= total(adopt_legal)
bysort year group_bound_cyp: egen legal_bound_cyp= total(adopt_legal)
bysort year group_bound_cze: egen legal_bound_cze= total(adopt_legal)
bysort year group_bound_den: egen legal_bound_den= total(adopt_legal)
bysort year group_bound_est: egen legal_bound_est= total(adopt_legal)
bysort year group_bound_fin: egen legal_bound_fin= total(adopt_legal)
bysort year group_bound_ger: egen legal_bound_ger= total(adopt_legal)
bysort year group_bound_gre: egen legal_bound_gre= total(adopt_legal)
bysort year group_bound_hun: egen legal_bound_hun= total(adopt_legal)
bysort year group_bound_ice: egen legal_bound_ice= total(adopt_legal)
bysort year group_bound_ita: egen legal_bound_ita= total(adopt_legal)
bysort year group_bound_lat: egen legal_bound_lat= total(adopt_legal)
bysort year group_bound_lit: egen legal_bound_lit= total(adopt_legal)
bysort year group_bound_lux: egen legal_bound_lux= total(adopt_legal)
bysort year group_bound_net: egen legal_bound_net= total(adopt_legal)
bysort year group_bound_nor: egen legal_bound_nor= total(adopt_legal)
bysort year group_bound_pol: egen legal_bound_pol= total(adopt_legal)
bysort year group_bound_por: egen legal_bound_por= total(adopt_legal)
bysort year group_bound_rom: egen legal_bound_rom= total(adopt_legal)
bysort year group_bound_slo: egen legal_bound_slo= total(adopt_legal)
bysort year group_bound_slove: egen legal_bound_slove= total(adopt_legal)
bysort year group_bound_spa: egen legal_bound_spa= total(adopt_legal)
bysort year group_bound_swe: egen legal_bound_swe= total(adopt_legal)
bysort year group_bound_swi: egen legal_bound_swi= total(adopt_legal)

gen legal_bound=.
replace legal_bound=legal_bound_aus if country=="Austria"
replace legal_bound=legal_bound_bel if country=="Belgium"
replace legal_bound=legal_bound_bul if country=="Bulgaria"
replace legal_bound=legal_bound_cro if country=="Croatia"
replace legal_bound=legal_bound_cyp if country=="Cyprus"
replace legal_bound=legal_bound_cze if country=="Czech Republic"
replace legal_bound=legal_bound_den if country=="Denmark"
replace legal_bound=legal_bound_est if country=="Estonia"
replace legal_bound=legal_bound_fin if country=="Finland"
replace legal_bound=legal_bound_ger if country=="Germany"
replace legal_bound=legal_bound_gre if country=="Greece"
replace legal_bound=legal_bound_hun if country=="Hungary"
replace legal_bound=legal_bound_ice if country=="Iceland"
replace legal_bound=legal_bound_ita if country=="Italy"
replace legal_bound=legal_bound_lat if country=="Latvia"
replace legal_bound=legal_bound_lit if country=="Lithuania"
replace legal_bound=legal_bound_lux if country=="Luxembourg"
replace legal_bound=legal_bound_net if country=="Netherlands"
replace legal_bound=legal_bound_nor if country=="Norway"
replace legal_bound=legal_bound_pol if country=="Poland"
replace legal_bound=legal_bound_por if country=="Portugal"
replace legal_bound=legal_bound_rom if country=="Romania"
replace legal_bound=legal_bound_slo if country=="Slovakia"
replace legal_bound=legal_bound_slove if country=="Slovenia"
replace legal_bound=legal_bound_spa if country=="Spain"
replace legal_bound=legal_bound_swe if country=="Sweden"
replace legal_bound=legal_bound_swi if country=="Switzerland"

replace legal_ling=legal_ling-1 if adopt_legal==1
replace legal_demo=legal_demo-1 if adopt_legal==1
replace legal_bound=legal_bound-1 if adopt_legal==1

replace legal_ling=legal_ling/n_peers_ling
replace legal_demo=legal_demo/n_peers_demo
replace legal_bound=legal_bound/n_peers_bound

* Variables: imit[time-span]_dm_ling, imit[time-span]_dm_demo, imit[time-span]_dm_bound, imit[time-span]_legal_ling, imit[time-span]_legal_demo, imit[time-span]_legal_bound
* Description: Cumulative sum of the proportion of linguistic, historical and geographical peers that have have adopted low district magnitudes and high elecotral thresholds within [time-span]

* 2 years
by country (year), sort: gen imit2_dm_ling = sum(dm_ling)-sum(dm_ling[_n-2])
by country (year), sort: gen imit2_dm_demo = sum(dm_demo)-sum(dm_demo[_n-2])
by country (year), sort: gen imit2_dm_bound = sum(dm_bound)-sum(dm_bound[_n-2])
by country (year), sort: gen imit2_legal_ling = sum(legal_ling)-sum(legal_ling[_n-2])
by country (year), sort: gen imit2_legal_demo = sum(legal_demo)-sum(legal_demo[_n-2])
by country (year), sort: gen imit2_legal_bound = sum(legal_bound)-sum(legal_bound[_n-2])

* 3 years
by country (year), sort: gen imit3_dm_ling = sum(dm_ling)-sum(dm_ling[_n-3])
by country (year), sort: gen imit3_dm_demo = sum(dm_demo)-sum(dm_demo[_n-3])
by country (year), sort: gen imit3_dm_bound = sum(dm_bound)-sum(dm_bound[_n-3])
by country (year), sort: gen imit3_legal_ling = sum(legal_ling)-sum(legal_ling[_n-3])
by country (year), sort: gen imit3_legal_demo = sum(legal_demo)-sum(legal_demo[_n-3])
by country (year), sort: gen imit3_legal_bound = sum(legal_bound)-sum(legal_bound[_n-3])

* 4 years
by country (year), sort: gen imit4_dm_ling = sum(dm_ling)-sum(dm_ling[_n-4])
by country (year), sort: gen imit4_dm_demo = sum(dm_demo)-sum(dm_demo[_n-4])
by country (year), sort: gen imit4_dm_bound = sum(dm_bound)-sum(dm_bound[_n-4])
by country (year), sort: gen imit4_legal_ling = sum(legal_ling)-sum(legal_ling[_n-4])
by country (year), sort: gen imit4_legal_demo = sum(legal_demo)-sum(legal_demo[_n-4])
by country (year), sort: gen imit4_legal_bound = sum(legal_bound)-sum(legal_bound[_n-4])

* 5 years
by country (year), sort: gen imit5_dm_ling = sum(dm_ling)-sum(dm_ling[_n-5])
by country (year), sort: gen imit5_dm_demo = sum(dm_demo)-sum(dm_demo[_n-5])
by country (year), sort: gen imit5_dm_bound = sum(dm_bound)-sum(dm_bound[_n-5])
by country (year), sort: gen imit5_legal_ling = sum(legal_ling)-sum(legal_ling[_n-5])
by country (year), sort: gen imit5_legal_demo = sum(legal_demo)-sum(legal_demo[_n-5])
by country (year), sort: gen imit5_legal_bound = sum(legal_bound)-sum(legal_bound[_n-5])

* 6 years
by country (year), sort: gen imit6_dm_ling = sum(dm_ling)-sum(dm_ling[_n-6])
by country (year), sort: gen imit6_dm_demo = sum(dm_demo)-sum(dm_demo[_n-6])
by country (year), sort: gen imit6_dm_bound = sum(dm_bound)-sum(dm_bound[_n-6])
by country (year), sort: gen imit6_legal_ling = sum(legal_ling)-sum(legal_ling[_n-6])
by country (year), sort: gen imit6_legal_demo = sum(legal_demo)-sum(legal_demo[_n-6])
by country (year), sort: gen imit6_legal_bound = sum(legal_bound)-sum(legal_bound[_n-6])

* 7 years
by country (year), sort: gen imit7_dm_ling = sum(dm_ling)-sum(dm_ling[_n-7])
by country (year), sort: gen imit7_dm_demo = sum(dm_demo)-sum(dm_demo[_n-7])
by country (year), sort: gen imit7_dm_bound = sum(dm_bound)-sum(dm_bound[_n-7])
by country (year), sort: gen imit7_legal_ling = sum(legal_ling)-sum(legal_ling[_n-7])
by country (year), sort: gen imit7_legal_demo = sum(legal_demo)-sum(legal_demo[_n-7])
by country (year), sort: gen imit7_legal_bound = sum(legal_bound)-sum(legal_bound[_n-7])

* 8 years
by country (year), sort: gen imit8_dm_ling = sum(dm_ling)-sum(dm_ling[_n-8])
by country (year), sort: gen imit8_dm_demo = sum(dm_demo)-sum(dm_demo[_n-8])
by country (year), sort: gen imit8_dm_bound = sum(dm_bound)-sum(dm_bound[_n-8])
by country (year), sort: gen imit8_legal_ling = sum(legal_ling)-sum(legal_ling[_n-8])
by country (year), sort: gen imit8_legal_demo = sum(legal_demo)-sum(legal_demo[_n-8])
by country (year), sort: gen imit8_legal_bound = sum(legal_bound)-sum(legal_bound[_n-8])

* 9 years
by country (year), sort: gen imit9_dm_ling = sum(dm_ling)-sum(dm_ling[_n-9])
by country (year), sort: gen imit9_dm_demo = sum(dm_demo)-sum(dm_demo[_n-9])
by country (year), sort: gen imit9_dm_bound = sum(dm_bound)-sum(dm_bound[_n-9])
by country (year), sort: gen imit9_legal_ling = sum(legal_ling)-sum(legal_ling[_n-9])
by country (year), sort: gen imit9_legal_demo = sum(legal_demo)-sum(legal_demo[_n-9])
by country (year), sort: gen imit9_legal_bound = sum(legal_bound)-sum(legal_bound[_n-9])

* 10 years
by country (year), sort: gen imit10_dm_ling = sum(dm_ling)-sum(dm_ling[_n-10])
by country (year), sort: gen imit10_dm_demo = sum(dm_demo)-sum(dm_demo[_n-10])
by country (year), sort: gen imit10_dm_bound = sum(dm_bound)-sum(dm_bound[_n-10])
by country (year), sort: gen imit10_legal_ling = sum(legal_ling)-sum(legal_ling[_n-10])
by country (year), sort: gen imit10_legal_demo = sum(legal_demo)-sum(legal_demo[_n-10])
by country (year), sort: gen imit10_legal_bound = sum(legal_bound)-sum(legal_bound[_n-10])

* Variables: lag_enpv, lag_enp_seat
* Description: Lagging covariates

sort country year 
by country: gen lag_enpv = enpv[_n-1]

sort country year 
by country: gen lag_enp_seat = enp_seat[_n-1]

sort country year 
by country: gen lag_majority = majority[_n-1]

sort country year 
by country: gen lag_parliamentary = parliamentary[_n-1]

sort country year 
by country: gen lag_federal = federal[_n-1]

sort country year 
by country: gen lag_parties_gov = parties_gov[_n-1]

sort country year 
by country: gen lag_constitution_pr = constitution_pr[_n-1]

***********
* Analyses*
***********

* Table 1
**********

bysort country: tab year dv_dm
bysort country: tab year dv_legal

* Table 2
**********

* Low district magnitude


* Without controls
logit dv_dm imit2_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_dm imit2_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_dm imit2_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
test lag_enpv lag_enp_seat

* With controls

logit dv_dm imit2_dm_bound lag_enpv lag_enp_seat lag_majority lag_parliamentary lag_federal lag_constitution_pr lag_parties_gov dv_legal year if election_year==1 & number_dm!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_dm imit2_dm_ling lag_enpv lag_enp_seat lag_majority lag_parliamentary lag_federal lag_constitution_pr lag_parties_gov dv_legal year if election_year==1 & number_dm!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_dm imit2_dm_demo lag_enpv lag_enp_seat lag_majority lag_parliamentary lag_federal lag_constitution_pr lag_parties_gov dv_legal year if election_year==1 & number_dm!=., cluster(country)
test lag_enpv lag_enp_seat

* High electoral threshold

* Without controls

logit dv_legal imit2_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_legal imit2_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_legal imit2_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
test lag_enpv lag_enp_seat

* With controls

logit dv_legal imit2_legal_bound lag_enpv lag_enp_seat lag_majority lag_parliamentary lag_federal lag_constitution_pr lag_parties_gov dv_dm year if election_year==1 & number_legal!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_legal imit2_legal_ling lag_enpv lag_enp_seat lag_majority lag_parliamentary lag_federal lag_constitution_pr lag_parties_gov dv_dm year if election_year==1 & number_legal!=., cluster(country)
test lag_enpv lag_enp_seat
logit dv_legal imit2_legal_demo lag_enpv lag_enp_seat lag_majority lag_parliamentary lag_federal lag_constitution_pr lag_parties_gov dv_dm year if election_year==1 & number_legal!=., cluster(country)
test lag_enpv lag_enp_seat

* Figure 1
**********

gen constant=1

gen dv_all=.
replace dv_all=1 if dv_dm==1
replace dv_all=1 if dv_legal==1

sort year
by year: egen total_dm=total(dv_dm)
sort year
by year: egen total_legal=total(dv_legal)
sort year
by year: egen total_all=total(dv_all)
sort year
by year: egen total_constant=total(constant)

gen prop_dm=total_dm/total_constant
gen prop_legal=total_legal/total_constant
gen prop_all=total_all/total_constant

line prop_dm prop_legal prop_all year if country=="Iceland"

* Figure 2
************

* Low district magnitude

logit dv_dm imit2_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit2_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit2_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit3_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit3_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit3_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit4_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit4_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit4_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit5_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit5_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit5_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit6_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit6_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit6_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit7_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit7_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit7_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit8_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit8_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit8_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit9_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit9_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit9_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

logit dv_dm imit10_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit10_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit10_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

* High electoral threshold

logit dv_legal imit2_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit2_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit2_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit3_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit3_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit3_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit4_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit4_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit4_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit5_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit5_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit5_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit6_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit6_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit6_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit7_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit7_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit7_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit8_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit8_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit8_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit9_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit9_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit9_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

logit dv_legal imit10_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit10_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)
logit dv_legal imit10_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=., cluster(country)

* Robutness checks appendix
****************************


* Old democracies only

logit dv_dm imit2_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=. & group_demo!=1, cluster(country)
logit dv_dm imit2_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=. & group_demo!=1, cluster(country)
logit dv_dm imit2_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=. & group_demo!=1, cluster(country)

logit dv_legal imit2_legal_bound lag_enpv lag_enp_seat if election_year==1 & number_legal!=. & group_demo!=1, cluster(country)
logit dv_legal imit2_legal_ling lag_enpv lag_enp_seat if election_year==1 & number_legal!=. & group_demo!=1, cluster(country)
logit dv_legal imit2_legal_demo lag_enpv lag_enp_seat if election_year==1 & number_legal!=. & group_demo!=1, cluster(country)

* Cox models

stset year, failure(dv_dm)

stcox imit2_dm_bound lag_enpv lag_enp_seat, strata(number_dm) efron cluster(country) robust nohr
stcox imit2_dm_ling lag_enpv lag_enp_seat, strata(number_dm) efron cluster(country) robust nohr
stcox imit2_dm_demo lag_enpv lag_enp_seat, strata(number_dm) efron cluster(country) robust nohr

stset year, failure(dv_legal)

stcox imit2_legal_bound lag_enpv lag_enp_seat, strata(number_legal) efron cluster(country) robust nohr
stcox imit2_legal_ling lag_enpv lag_enp_seat, strata(number_legal) efron cluster(country) robust nohr
stcox imit2_legal_demo lag_enpv lag_enp_seat, strata(number_legal) efron cluster(country) robust nohr


* Without contentious multi-tier systems

drop if country=="Denmark"
drop if country=="Iceland"
drop if country=="Poland" & year>1992 & year<2000

logit dv_dm imit2_dm_bound lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit2_dm_ling lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)
logit dv_dm imit2_dm_demo lag_enpv lag_enp_seat if election_year==1 & number_dm!=., cluster(country)

